package utils;

import java.sql.*;

/**
 * @author konley
 * @date 2020-06-09 17:58
 * jdbc工具类
 */
public class DBUtils {
    //获取Connection
    public static Connection getConnection(){
        Connection con = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/db_card?useUnicode=true&characterEncoding=utf8";
            String name = "root";
            String psw = "123456";
            con = DriverManager.getConnection(url,name,psw);
        }catch (ClassNotFoundException | SQLException e){
            System.out.println("数据库异常："+e.getMessage());
        }
        return con;
    }
    //回收1
    public static void release(Statement stmt, Connection conn) {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            stmt = null;
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            conn = null;
        }
    }
    //回收2
    public static void release(ResultSet rs, Statement stmt, Connection conn) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            rs = null;
        }
        release(stmt, conn);
    }

}
